package haha.life.normal;


import jzqb.po.AdminDepartmentPo;
import jzqb.po.ZtreeJsonPo;

import java.util.*;


public class ZtreeJsonUtils {

    /*
    * deptList:通过查询直接得到的部门对象的集合(layui-tree:废弃)
    * */
    public static ArrayList<ZtreeJsonPo> generateZtreeJson(List<AdminDepartmentPo> deptList){

//        Map map = new HashMap();
//        for (int i = 0; i < deptList.size(); i++) {
//            map.put(i,deptList.get(i));
//        }

        ArrayList<ZtreeJsonPo> newDeptList = new ArrayList<>();
        for (AdminDepartmentPo po:deptList){
            ZtreeJsonPo po_ = new ZtreeJsonPo();
            po_.setTitle(po.getDepartmentName());
            po_.setId(Integer.parseInt(po.getDepartmentCode()));
            po_.setPid(Integer.parseInt(po.getPDepartmentCode()));
            po_.setField("");
            po_.setDeep(0);
            po_.setChecked(false);
            po_.setSpread(true);
            newDeptList.add(po_);
        }

        ArrayList<ZtreeJsonPo>  jsonList = getThree(newDeptList,0);

        return jsonList;



//        return "sb.toString()";
    }

    private static ArrayList<ZtreeJsonPo> getThree(List<ZtreeJsonPo> list,int parentId){
        //获取所有子节点
        ArrayList<ZtreeJsonPo> childTreeList = getChildTree(list,parentId);
        for (ZtreeJsonPo dept:childTreeList) {
            dept.setChildren(getThree(list,dept.getId()));
        }
        return childTreeList;
    }

    private static ArrayList<ZtreeJsonPo> getChildTree(List<ZtreeJsonPo> list,int parentId){
        ArrayList<ZtreeJsonPo> childTree = new ArrayList<>();
        for (ZtreeJsonPo ztreePo:list){
            if (ztreePo.getPid()==parentId){
                childTree.add(ztreePo);
            }
        }
        return childTree;
    }
}
